#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "../include/bomb.h"
#include "../include/map.h"
#include "../include/constant.h"


struct s_bomb
{
	int x, y;
	int power;
	Uint32 time_;
	SDL_Surface * directed_img[7];
	int bomb_lifetime;

	enum e_time_bomb current_timebomb;
	enum e_explode_bomb current_explodebomb;
};

//create new bomb
t_bomb bomb_new(int x, int y, int power)
{
	t_bomb bomb = malloc (sizeof(*bomb));
	bomb->x = x;
	bomb->y = y;
	bomb->power = power;
	bomb->time_ = SDL_GetTicks();
	bomb->bomb_lifetime = BOMB_LIFETIME;

	bomb->directed_img[TIMEBOMB_1] = load_image(IMG_BOMB_TTL1);
	bomb->directed_img[TIMEBOMB_2] = load_image(IMG_BOMB_TTL2);
	bomb->directed_img[TIMEBOMB_3] = load_image(IMG_BOMB_TTL3);
	bomb->directed_img[TIMEBOMB_4] = load_image(IMG_BOMB_TTL4);
	bomb->directed_img[EXPLODEBOMB_0] = load_image(IMG_BOMBEXPLODE_0);
	bomb->directed_img[EXPLODEBOMB_1] = load_image(IMG_BOMBEXPLODE_1);
	bomb->directed_img[EXPLODEBOMB_2] = load_image(IMG_BOMBEXPLODE_2);

	bomb->current_timebomb = TIMEBOMB_4;
	bomb->current_explodebomb = EXPLODEBOMB_1;
	return bomb;
}

/*void bomb_life (t_bomb bomb)
{
	if( bomb->time_ = 1 ){
		bomb_set_current_timebomb(bomb, TIMEBOMB_3);
	}
}*/

void bomb_set_current_timebomb(t_bomb bomb, enum e_time_bomb timebomb)
{
	assert(bomb);
	bomb->current_timebomb = timebomb;
}

/*Pour l'instant je n'utilise pas cette fonction mais au cas ou je la garde*/
void bomb_set_current_explodebomb(t_bomb bomb, enum e_explode_bomb explodebomb)
{
	assert(bomb);
	bomb->current_explodebomb = explodebomb;
}

int bomb_dec_lifetime(t_bomb bomb)
{
	assert(bomb);
	return bomb->bomb_lifetime--;
}

int bomb_display(t_bomb bomb, SDL_Surface *screen)
{

	SDL_Rect place;			//where the player is
	place.x = bomb->x * SIZE_BLOC;
	place.y = bomb->y * SIZE_BLOC;

	SDL_Rect place1;		//top
	place1.x = bomb->x * SIZE_BLOC;
	place1.y = (bomb->y -1) * SIZE_BLOC;

	SDL_Rect place2;		//down
	place2.x = bomb->x * SIZE_BLOC;
	place2.y = (bomb->y +1) * SIZE_BLOC;

	SDL_Rect place3;		//right
	place3.x = (bomb->x +1)* SIZE_BLOC;
	place3.y = bomb->y * SIZE_BLOC;

	SDL_Rect place4;		//left
	place4.x = (bomb->x -1)* SIZE_BLOC;
	place4.y = bomb->y * SIZE_BLOC;


	SDL_BlitSurface(bomb->directed_img[bomb->current_timebomb],NULL, screen, &place);

	Uint32 diff1 = SDL_GetTicks() - bomb->time_;
	if( diff1 > 1000 && diff1 <2000){
		bomb_set_current_timebomb(bomb, TIMEBOMB_3);
		//bomb_dec_lifetime(bomb);
	}
	//Uint32 diff2 = SDL_GetTicks() - bomb->time_;		USELESS
	if( diff1 > 2000 && diff1 <3000){
		bomb_set_current_timebomb(bomb, TIMEBOMB_2);
		//bomb_dec_lifetime(bomb);
	}
	//Uint32 diff3 = SDL_GetTicks() - bomb->time_;		USELESS
	if( diff1 > 3000 && diff1 <4000){
		bomb_set_current_timebomb(bomb, TIMEBOMB_1);
		//bomb_dec_lifetime(bomb);
	}
	//Uint32 diff4 = SDL_GetTicks() - bomb->time_;		USELESS
	if ( diff1 > 4000 && diff1 <4050){
		bomb_set_current_timebomb(bomb, EXPLODEBOMB_0 );

	}
	//Uint32 diff5 = SDL_GetTicks() - bomb->time_;		USELESS
	if (diff1 > 4100 && diff1 < 4400 ){
		SDL_BlitSurface(bomb->directed_img[EXPLODEBOMB_1],NULL, screen, &place1); //top
		SDL_BlitSurface(bomb->directed_img[EXPLODEBOMB_1],NULL, screen, &place2); //down
		SDL_BlitSurface(bomb->directed_img[EXPLODEBOMB_2],NULL, screen, &place3); //right
		SDL_BlitSurface(bomb->directed_img[EXPLODEBOMB_2],NULL, screen, &place4); //left
		//bomb_dec_lifetime(bomb);
		bomb->bomb_lifetime = 0;

	}
	int x = 2;
	if(diff1 > 4200 && diff1 < 4500){
		if(bomb->bomb_lifetime == 0) x=0;
		else x=1;
		//bomb = NULL;
	}

	return x;
}
